const axios = require('axios')
// const Vue = require('vue')
const { Message } = require('element-ui');
const router = require('../../router/index');

module.exports = {
    namespaced: true,
    state: {
        ordersData: [],
        ordersLoseData:[],
        ordersshippedData:[],
        ordersdelayedData:[],
        form: {
            _id: '',
            num: '',
            address: '',
            price: ''
        },
        dialogFormVisible:false,
        hide:true,
        addOrdersData:{
            name:'',
            num:'',
            price:'',
            
        }
        
    },
    mutations: {
        getOrders(state, data) {
            // if(data.type==success){
            state.ordersData = data
        },
        getOrdersLose(state,data){
            state.ordersLoseData=data
        },
        getOrdersshipped(state,data){
            state.ordersshippedData=data
        },
        getOrdersdelayed(state,data){
            state.ordersdelayedData=data
        },
        updateOrders(state,row){
            state.form={...row}
            state.dialogFormVisible=true
        },
        close(state,row){
            state.dialogFormVisible=false
        },
      
        
    },
    actions: {
        //获取有效订单
        async getOrdersAsync(context) {
            const { data } = await axios.get('/orders/getOrders',{params:{state:'有效'}})
                context.commit('getOrders', data)    
            
        },
        //失效订单
        async getOrdersLoseAsync(context) {
            const { data } = await axios.get('/orders/getOrders',{params:{state:'失效'}})
                context.commit('getOrdersLose', data)              
        },
        //已发货商品
        async getOrdersshippedAsync(context) {
            const { data } = await axios.get('/orders/getOrders',{params:{state:'发货'}})
                context.commit('getOrdersshipped', data)              
        },
        //延长收货
        async getOrdersdelayedAsync(context) {
            
            const { data } = await axios.get('/orders/getOrders',{params:{state:'延发'}})
                context.commit('getOrdersdelayed', data)              
        },
        
        //修改订单
        async updateOrdersAsync(context,_id){
            const {data}=await axios.post('/orders/updateOrders',context.state.form)
            context.state.dialogFormVisible=false
            context.dispatch('getOrdersAsync')
        },
        //取消订单
        async deletOrdersAsync(context,order){
            if(order.state=='失效'){
                order.state='有效'
            }else if(order.state=='有效'){
                order.state='失效'
            }else if(order.state=='延发'){
                order.state='失效'
            }
            
            console.info(order.state)
            const {data}=await axios.post('/orders/updateOrders',order)
            context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersdelayedAsync')         
            // context.dispatch('getOrdersshippedAsync')         

        },
        async shippedOrdersAsync(context,order){
           order.state='发货'
            
            console.info(order.state)
            const {data}=await axios.post('/orders/updateOrders',order)
            // context.dispatch('getOrdersLoseAsync')
            context.dispatch('getOrdersAsync')
            context.dispatch('getOrdersshippedAsync')  
            context.dispatch('getOrdersdelayedAsync')         


        },
        async delayedOrdersAsync(context,order){
             order.state='延发'   
             console.info(order.state)
             const {data}=await axios.post('/orders/updateOrders',order)
             // context.dispatch('getOrdersLoseAsync')
             context.dispatch('getOrdersAsync')
             context.dispatch('getOrdersdelayedAsync')         
            //  context.dispatch('getOrdersshippedAsync')         
 
         },
        async addOrdersAsync(context){
            const {data}=await axios.post('/orders/addOrders',context.state.addOrdersData)
            // console.info(data)
        }

    }
}